package com.example.teaching_server.repository;

import com.example.teaching_server.entity.CourseRecording;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程录制数据访问层
 */
@Repository
public interface CourseRecordingRepository extends JpaRepository<CourseRecording, Long> {

    /**
     * 根据班级名称和日期范围查询录制记录
     */
    @Query("SELECT r FROM CourseRecording r WHERE " +
            "(cast(:className as string) IS NULL OR r.className = :className) AND " +
            "(cast(:startDate as timestamp) IS NULL OR r.startTime >= :startDate) AND " +
            "(cast(:endDate as timestamp) IS NULL OR r.startTime <= :endDate) " +
            "ORDER BY r.startTime DESC")
    List<CourseRecording> findRecordings(
            @Param("className") String className,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);
}
